﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using XukaShop.Models.Entities;

namespace XukaShop.Dao
{
    public class BrandDao
    {
        private FashionShopEntities _db = new FashionShopEntities();

        /// <summary>
        /// Get List Product Brand in Database
        /// </summary>
        /// <returns></returns>
        public List<Brand> GetListBrands()
        {
            var selectBrands = (from brand in _db.Brand
                                where brand.IsDeleted == false
                                select brand).ToList();
            return selectBrands;
        }

        /// <summary>
        /// Get a Product Brand by Id
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public Brand GetBrandById(int brandId)
        {
            var selectBrand = (from brand in _db.Brand
                               where brand.Id == brandId
                                     && brand.IsDeleted == false
                               select brand).FirstOrDefault();
            return selectBrand;
        }

        /// <summary>
        /// Edit a Product Brand
        /// </summary>
        /// <param name="brandModel"></param>
        /// <returns></returns>
        public bool EditBrand(Brand brandModel)
        {
            _db.Brand.Attach(brandModel);
            _db.Entry(brandModel).State = EntityState.Modified;
            var queryResult = _db.SaveChanges();
            if (queryResult > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Create a Product Brand
        /// </summary>
        /// <param name="brandModel"></param>
        /// <returns></returns>
        public bool CreateBrand(Brand brandModel)
        {
            _db.Brand.Add(brandModel);
            var queryResult = _db.SaveChanges();
            if (queryResult > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Delete a Product Brand
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns>Id of brand</returns>
        public bool DeleteBrand(int brandId)
        {
            Brand brandModel = new Brand();
            brandModel = (from brand in _db.Brand
                          where brand.Id == brandId
                          select brand).FirstOrDefault();
            if (brandModel != null)
            {
                brandModel.IsDeleted = true;
                _db.Brand.Attach(brandModel);
                _db.Entry(brandModel).State = EntityState.Modified;
                var queryResult = _db.SaveChanges();
                if (queryResult > 0)
                {
                    return true;
                }
                return false;
            }
            return false;
        }

        /// <summary>
        /// Check Brand is existed nor not
        /// </summary>
        /// <param name="brandName"></param>
        /// <param name="isUpdateMode">Check when Update or Create</param>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public bool IsExistedBrand(string brandName, bool isUpdateMode, int brandId = 0)
        {
            if (isUpdateMode)
            {
                return _db.Brand.Any(model => model.BrandName == brandName && model.Id != brandId);
            }
            return _db.Brand.Any(model => model.BrandName == brandName);
        }

    }
}